package com.wus.common.web.exception;

import com.wus.common.core.domain.base.Result;
import com.wus.common.core.exception.BusinessException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

/**
 * @Description: 接口统一异常处理类
 * @Author: wus
 * @Date: 2024/12/12 17:33
 */
@RestControllerAdvice
public class WebCustomExceptionHandler {

    private final Logger logger = LoggerFactory.getLogger(WebCustomExceptionHandler.class);

    /**
     * 业务异常处理
     * @param ex
     * @return
     */
    @ExceptionHandler(value = BusinessException.class)
    public Result<String> handleBusinessException(BusinessException ex) {
        this.printExceptionMsg(ex);
        return Result.fail(ex.getMessage());
    }

    /**
     * 400 - Bad Request
     * 参数绑定校验异常捕捉处理
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Result<String> handleMethodArgumentNotValidException(MethodArgumentNotValidException ex) {
        this.printExceptionMsg(ex);
        BindingResult bindingResult = ex.getBindingResult();
        StringBuilder msgSb = new StringBuilder();
        for (FieldError error : bindingResult.getFieldErrors()) {
            if (!msgSb.isEmpty()) {
                msgSb.append(",");
            }
            msgSb.append(error.getDefaultMessage());
        }
        return Result.fail(HttpStatus.BAD_REQUEST.value(), msgSb.toString());
    }

    /**
     * 400 - Bad Request
     * 参数绑定校验异常捕捉处理
     */
    @ExceptionHandler(BindException.class)
    public Result<String> handleBindException(BindException ex) {
        this.printExceptionMsg(ex);
        // FieldError fieldError = ex.getFieldError();
        return Result.fail(HttpStatus.BAD_REQUEST.value(), ex.getMessage());
    }

    /**
     * 404 - Not Found
     */
    @ExceptionHandler(NoHandlerFoundException.class)
    public Result<String> noHandlerFoundException(NoHandlerFoundException ex) {
        this.printExceptionMsg(ex);
        return Result.fail(HttpStatus.NOT_FOUND.value(), ex.getMessage());
    }

    /**
     * 405 - Method Not Allowed
     */
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public Result<String> handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException ex) {
        this.printExceptionMsg(ex);
        return Result.fail(HttpStatus.METHOD_NOT_ALLOWED.value(), ex.getMessage());
    }

    /**
     * 全局异常捕捉处理
     * @param ex
     * @return
     */
    @ExceptionHandler(value = Exception.class)
    public Result<String> handleOtherException(Exception ex) {
        this.printExceptionMsg(ex);
        return Result.fail(HttpStatus.INTERNAL_SERVER_ERROR.value(), ex.getMessage());
    }

    private void printExceptionMsg(Exception ex) {
        logger.error("异常信息：", ex);
    }
}
